﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
using socialnexus.classes.models;
using socialnexus.classes.pages;

namespace socialnexus
{
    public partial class Inbox : AuthenticatedUserPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            SiteUser su = GetUser();

            string FriendMsgs = @"SELECT SiteUsers.SiteUserId, SiteUsers.PictureURL, 
                    ISNULL(M.MessageId, 0) AS MessageId, ISNULL(M.MsgCount, 0) AS MsgCount
                FROM Friends 
                INNER JOIN SiteUsers ON SiteUsers.SiteUserId = Friends.User2Id
                LEFT JOIN (
	                SELECT DISTINCT 
                        MAX(MessageId) OVER(PARTITION BY Messages.FromUserId) MessageId, 
                        COUNT(MessageId) OVER(PARTITION BY Messages.FromUserId) MsgCount,
		                FromUserId
	                FROM Messages
	                WHERE IsRead = 0 AND ToUserId = @ID
                ) M ON M.FromUserId = SiteUsers.SiteUserId
                WHERE User1Id = @ID AND IsActive = 1 
                ORDER BY MessageId DESC";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["socialnexusConnectionString"].ConnectionString);
            con.Open();
            SqlCommand cmd = new SqlCommand(FriendMsgs, con);
            cmd.Parameters.Add(new SqlParameter("@ID", su.SiteUserId));

            SqlDataReader reader = cmd.ExecuteReader();

            rptrInbox.DataSource = reader;
            rptrInbox.DataBind();
        }

        protected void rptrInbox_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            System.Data.Common.DbDataRecord reader = (System.Data.Common.DbDataRecord)e.Item.DataItem;

            HyperLink hlMessage = (HyperLink)e.Item.FindControl("hlMessage");
            Label lblMessageCount = (Label)e.Item.FindControl("lblMessageCount");
            System.Web.UI.WebControls.Image imgFriend = (System.Web.UI.WebControls.Image)e.Item.FindControl("imgFriend");

            if (reader["MessageId"].ToString() == "0")
            {
                hlMessage.NavigateUrl = "~/SendMessage.aspx?FriendId=" + reader["SiteUserId"].ToString();
                hlMessage.CssClass = "noMessage";
            }
            else
            {
                hlMessage.NavigateUrl = "~/ViewMessage.aspx?MessageId=" + reader["MessageId"].ToString();
                hlMessage.CssClass = "withMessage";

                lblMessageCount.Text = reader["MsgCount"].ToString();
                lblMessageCount.CssClass = "messageCount";
            }

            imgFriend.ImageUrl = reader["PictureURL"].ToString().Trim();
        }
    }
}
